package com.ironone.mediaplayerdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.Button;
import android.widget.FrameLayout;

/*
 * 
 * Just make sure you change all calls to
 * getLoaderManager() and getFragmentManager() to 
 * getSupportLoaderManager() and getSupportFragmentManager() respectively.
 * 
 */

public class SecondActivity extends FragmentActivity {

	ViewPager viewPager;
	PagerAdapter pagerAdapter;
	FrameLayout fragContainer;
	Button dragIcon;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second_activity);

		//Button previousButton = (Button) findViewById(R.id.previous_button);
		fragContainer = (FrameLayout) findViewById(R.id.fragContainer);
		
		viewPager = (ViewPager) findViewById(R.id.pager);
		//viewPager.setPageTransformer(true, new DepthPageTransformer());
		pagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager()); //see below
		viewPager.setAdapter(pagerAdapter);

		
		
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				
				switch (arg0) {
				case 0: //menu
					//WORKS!!!
					//Frag1.updateFromActivity();
					break;

				case 1: //player
					break;

				case 2:  //weekly pick
					break;

				default:
					break;
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				int pageWidth = 0;
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				int pageWidth = 0;
			}
		});



		viewPager.setCurrentItem(0);
		/*
		previousButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//Intent i = new Intent(SecondActivity.this, MainActivity.class);
				//startActivity(i);
				//overridePendingTransition(R.anim.slide_right_to_middle, R.anim.slide_middle_to_left);
				//finish();			
			}
		});*/
	}

	//http://androidtrainningcenter.blogspot.com/2012/12/swipe-screens-in-android-viewpager.html
	//http://stackoverflow.com/questions/21360126/how-to-open-another-xml-page-by-sliding-left-to-right


	
	
	
	private class MyViewPagerAdapter extends FragmentStatePagerAdapter{

		public MyViewPagerAdapter(FragmentManager fm){
			super(fm);
		}

		@Override
		public int getCount(){
			return 2; //place the number of pages you want, in this case, only two pages
		}

		@Override
		public Fragment getItem(int position){
			//position count starts from 0, so if you have 3 pages, the count will be 0, 1 and 2
			if(position == 0){ // first page 
				//return new Frag1();  // show first page (menu screen...)
				return new Frag2(); // show second page
			}

			else if(position == 1){ // second page (playing screen...)
				//return new Frag2(); // show second page
				return new Frag1();  // show first page (menu screen...)
				
			}

			//keep doing the same thing for how many pages you have
			else{
				return null; //show a null page, generally you will never reach here
			}
		}
	}

	/*private class DepthPageTransformer implements ViewPager.PageTransformer {
		private static final float MIN_SCALE = 0.75f;

		public void transformPage(View view, float position) {
			int pageWidth = view.getWidth();

			if (position < -1) { // [-Infinity,-1)
				// This page is way off-screen to the left.
				view.setAlpha(0);

			} else if (position <= 0) { // [-1,0]
				// Use the default slide transition when moving to the left page
				view.setAlpha(1);
				view.setTranslationX(0);
				view.setScaleX(1);
				view.setScaleY(1);

			} else if (position <= 1) { // (0,1]
				// Fade the page out.
				view.setAlpha(1 - position);

				// Counteract the default slide transition
				view.setTranslationX(pageWidth * -position);

				// Scale the page down (between MIN_SCALE and 1)
				float scaleFactor = MIN_SCALE
						+ (1 - MIN_SCALE) * (1 - Math.abs(position));
				view.setScaleX(scaleFactor);
				view.setScaleY(scaleFactor);

			} else { // (1,+Infinity]
				// This page is way off-screen to the right.
				view.setAlpha(0);
			}
		}
	}*/
}
